package fr.pud.client.controller.bubble;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JOptionPane;

import fr.pud.client.i18n.LanguagesConstants;
import fr.pud.client.images.ImagesDatabase;
import fr.pud.client.model.PUDStation;

/**
 * The controller to show the information concerning the client linked to the
 * station.
 * Apr 8, 2012 - OSE
 * @author Simon Devineau, Adrien Hierry, Leopold Houdin, Pierre Reliquet
 *         Ecole des Mines de Nantes
 *         ShowClientInformation.java
 */
public class ShowClientInformation extends AbstractAction {
    /**
     * The station linked
     */
    private PUDStation        station;
    /**
     * The serial UID
     */
    private static final long serialVersionUID = 4718529009734944617L;

    /**
     * The default constructor
     * @param station
     *            , the station to link
     */
    public ShowClientInformation(PUDStation station) {
        this.putValue(NAME, LanguagesConstants.LANGUAGE
                .getString(LanguagesConstants.CLIENT));
        this.putValue(SMALL_ICON, ImagesDatabase.ADDRESSES);
        this.putValue(SHORT_DESCRIPTION, LanguagesConstants.LANGUAGE
                .getString(LanguagesConstants.CLIENT_DESCRIPTION));
        this.station = station;
    }

    /**
     * Display the information about the station linked.
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        JOptionPane.showMessageDialog(null, this.prepareString(),
                LanguagesConstants.LANGUAGE
                        .getString(LanguagesConstants.CLIENT_INFORMATION),
                JOptionPane.INFORMATION_MESSAGE, ImagesDatabase.ADDRESSES);
    }

    /**
     * @return a String representation of the client.
     */
    private String prepareString() {
        String toDisplay = "<html> ";
        if (this.station != null) {
            toDisplay += this.station.toHTMLWithColors();
            toDisplay = toDisplay.replaceFirst("\n", "<br/>");
        }
        else {
            toDisplay += LanguagesConstants.LANGUAGE
                    .getString(LanguagesConstants.ERROR_OCCURED);
        }
        toDisplay += " </html>";
        return toDisplay;
    }

    /**
     * Link a new station to the current controller.
     * @param station
     *            the new station to link.
     */
    public void setStation(PUDStation station) {
        this.station = station;
    }
}
